package org.rcgwt.client.ui.grid.provider;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.rcgwt.client.ui.grid.model.BasicColumn;
import org.rcgwt.client.ui.grid.model.IColumn;
import org.rcgwt.client.ui.grid.model.IDataModel;

/**
 * 
 * @author Olivier Oeuillot (latest modification by $Author: $)
 * @version $Revision: $ $Date: $
 */
public class BasicColumnProvider implements IColumnProvider {
    private static final String DEFAULT_TITLE_ROW_NAME = "default";

    private List<IColumn> columns = new ArrayList<IColumn>();

    private Iterator<IColumn> columnIterator = null;

    private int titleRowCount;

    protected IDataModel dataModel;

    public IColumn addColumn(String name, String title) {
        IColumn newColumn = new BasicColumn(name, title);
        addColumn(newColumn);
        return newColumn;
    }

    public void addColumn(IColumn column) {
        if (columns == null) {
            columns = new ArrayList<IColumn>();
        }
        if (columns.contains(column)) {
            throw new IllegalArgumentException("Column name already exists");
        }

        columns.add(column);
    }

    @Override
    public IColumn getColumnByModelIndex(int columnIndex) {

        if (columnIndex < columns.size()) {
            return columns.get(columnIndex);
        }

        return null;
    }

    @Override
    public int getTitleRowCount() {
        return titleRowCount;
    }

    @Override
    public int getColumnCount() {
        if (columns == null) {
            return 0;
        }

        return columns.size();
    }

    @Override
    public void setDataModel(IDataModel dataModel) {
        this.dataModel = dataModel;
    }

    @Override
    public Iterator<Integer> getColumnModelIndexes() {

        return new Iterator<Integer>() {

            private int columnModelIndex = 0;

            @Override
            public boolean hasNext() {
                return columnModelIndex < columns.size();
            }

            @SuppressWarnings("boxing")
            @Override
            public Integer next() {
                int ret = columnModelIndex++;

                return ret;
            }

            @Override
            public void remove() {
                throw new UnsupportedOperationException("Not supported !");
            }
        };
    }

    @Override
    public Iterator<Integer> getColumnModelIndexes(final int titleRowModelIndex) {

        return new Iterator<Integer>() {

            private int columnModelIndex = 0;

            @Override
            public boolean hasNext() {
                return columnModelIndex < columns.size();
            }

            @SuppressWarnings("boxing")
            @Override
            public Integer next() {
                int ret = columnModelIndex;

                IColumn current = columns.get(columnModelIndex);

                int colspan = current.getTitleColSpan(titleRowModelIndex);
                if (colspan < 2) {
                    colspan = 1;
                }

                columnModelIndex += colspan;

                return ret;
            }

            @Override
            public void remove() {
                throw new UnsupportedOperationException("Not supported !");
            }
        };
    }
}
